#include <iostream>
#include <string.h>

using namespace std;

void printArray(char *string)
{
  for(int i = 0; i < strlen(string); i++)
  {
    cout << string[i];
  }
  cout << endl;
}

void printString(string myString)
{
  cout << myString << endl;
  for(int i = 0; i < myString.size(); i++)
  {
    cout << myString[i];
  }
  cout << endl;
}

int main()
{
  char s1[] = "c string s1";

  printArray(s1);

  string s2;
  s2 = "c++ string s2";
  cout << s2 << endl;

  string s3("c++ string s3");
  cout << s3 << endl;

  //append
  s3.append(" Appended.");
  cout << s3 << endl;

  s3.append("0123456789", 0, 5);
  cout << s3 << endl;

  s3.append(5, 'G');
  cout << s3 << endl;

  //assign
  string s4("c++ string s4");
  cout << s4 << endl;

  s4.assign("aaaaa");
  cout << s4 << endl;

  string s5("c++ string s5");
  s5.assign("0123456789", 0, 5);
  cout << s5 << endl;

  string s6("c++ string s6");
  s6.assign(5, 'g');
  cout << s6 << endl;

  //at
  string s7 = "0123456789, c++ string s7";
  cout << s7.at(0) << endl;
  cout << s7.at(1) << endl;
  //erase
  s7.erase(1,3);
  cout << s7 << endl;
  //clear
  s7.clear();
  cout << "After s7.clear(), s7 = " << s7 << endl;
  //empty
  s7.assign("0123456789, c++ string s7");
  cout << s7 << endl;
  cout << "s7.empty() = " << s7.empty() << endl;
  cout << "After s7.empty(), s7 = " << s7 << endl;

  //length()
  string s8("0123456789, c++ string s8");
  cout << "s8.length() = " << s8.length() << endl;
  //size(), same as length()
  cout << "s8.size() = " << s8.size() << endl;
  //capacity()
  cout << "s8.capacity() = " << s8.capacity() << endl;

  //erase
  s8.erase(0, 5);
  cout << "s8.length() = " << s8.length() << endl;
  //size(), same as length()
  cout << "s8.size() = " << s8.size() << endl;
  //capacity()
  cout << "s8.capacity() = " << s8.capacity() << endl;

  string s9("012");
  string s10("013");
  string s11("014");
  cout << "s9.compare(s10) = " << s9.compare(s10) << endl;
  cout << "s10.compare(s9) = " << s10.compare(s9) << endl;
  cout << "s9.compare(s11) = " << s9.compare(s11) << endl;
  cout << "s9.compare(s9) = " << s9.compare(s9) << endl;
  cout << "s10.compare(\"013\") = " << s10.compare("013") << endl;

  //substring
  string s12("0123456789");
  cout << "s12(\"0123456789\")" << endl;
  cout << "s12.substr(0, 2) = " << s12.substr(0, 2) << endl;
  cout << "s12.substr(9, 1) = " << s12.substr(9, 1) << endl;
  cout << "s12.substr(1) = " << s12.substr(1) << endl;

  //searching, find()
  string s13("0123456789");
  cout << "s13.find(\"78\") = " << s13.find("78") << endl;
  cout << "s13.find(\"78\", 8) = " << s13.find("78", 8) << endl;
  cout << "s13.find(\"78\", 9) = " << s13.find("78", 9) << endl;
  cout << "( s13.find(\"78\", 8) == string::npos ) = " <<  (s13.find("78", 8) == string::npos) << endl;
  cout << "s13.find(\"78\", 1) = " << s13.find("78", 1) << endl;
  cout << "s13.find('5') = " << s13.find('5') << endl;
  cout << "s13.find('5', 4) = " << s13.find('5', 4) << endl;
  cout << "( s13.find('5', 7) == string::npos ) = " << (s13.find('5', 7) == string::npos ) << endl;
 
  //inserting
  string s14("0123456789");
  cout << "s14 = " << s14 << endl;
  s14.insert(5, "abc");
  cout << "s14.insert(5, \"abc\"), s14 = " << s14 << endl;
  s14.insert(2, 5, 'c');
  cout << "s14.insert(2, 5, 'c') = " << s14 << endl;

  //replace
  string s15("0123456789");
  cout << "s15 = " << s15 << endl;
  s15.replace(6, 2, "abcdefg");
  cout << "s15.replace(6, 2, \"abcdefg\") = " << s15 << endl;
 
  //print string, using []
  cout << "printString(s15) = " << endl;
  printString(s15);

  // +
  string s16("0123");
  s16 += "abcd";
  cout << s16 << endl;

  // ==
  string s17("01");
  string s18("02");
  cout << "s17 == s18 = " << (s17 == s18) << endl;
  cout << "s17 == \"01\" = " << (s17 == "01") << endl;

  // reading string from input
  string s19;
  cout << "Please input your string: ";
  cin >> s19;
  cout << "s19 = " << s19 << endl;

  return 0;
}
